// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	10/19/2014 6:13:13 PM				
// -----------------------------

#include "FreeCamera.h"

#include "Application.h"
#include "InputManager.h"

FreeCamera::FreeCamera(const TString& Name)
	: Camera(Name)
	, m_Yaw(0.0f)
	, m_Pitch(0.0f)
	, m_Speed(50.0f)
{
}

FreeCamera::~FreeCamera()
{
}

void FreeCamera::Update(Application* lpApp, Float deltaTime)
{
	Float rotSpeed = 2.0f * deltaTime;

	if (lpApp->GetInput()->IsKeyDown(VK_RBUTTON))
	{
		Vector2 mousePos = lpApp->GetInput()->GetMousePosition();
		Vector2 lastMousePos = lpApp->GetInput()->GetPreviousMousePosition();
		Vector2 deltaMousePos = mousePos - lastMousePos;
		if (Math::Abs(deltaMousePos.x) > 0.0f)
			m_Yaw -= deltaMousePos.x * rotSpeed;
		if (Math::Abs(deltaMousePos.y) > 0.0f)
			m_Pitch -= deltaMousePos.y * rotSpeed;
	}

	m_Rotation = Matrix::RotationX(m_Pitch) * Matrix::RotationY(m_Yaw);

	Vector3 pos = GetPosition();
	Float speed = m_Speed * deltaTime;

	if (lpApp->GetInput()->IsKeyDown(VK_LCONTROL))
		speed *= 10.0f;

	if (lpApp->GetInput()->IsKeyDown('D'))
		pos += GetRight() * speed;
	else if (lpApp->GetInput()->IsKeyDown('Q'))
		pos -= GetRight() * speed;

	if (lpApp->GetInput()->IsKeyDown('Z'))
		pos += GetForward() * speed;
	else if (lpApp->GetInput()->IsKeyDown('S'))
		pos -= GetForward() * speed;

	if (lpApp->GetInput()->IsKeyDown(VK_SPACE))
		pos += Vector3::UnitY * speed;
	else if (lpApp->GetInput()->IsKeyDown(VK_LSHIFT))
		pos -= Vector3::UnitY * speed;

	SetPosition(pos);

	Camera::Update(lpApp, deltaTime);
}

void FreeCamera::SetSpeed(Float speed)
{
	m_Speed = speed;
}